package com.hebust.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hebust.entity.Admins;
import com.hebust.mapper.AdminMapper;
import com.hebust.mapper.AdminsMapper;
import com.hebust.service.AdminsService;
import com.hebust.utils.ResultVo;
import com.hebust.utils.admin.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service   // 业务层 都需要加@Service 注解
public class AdminsServiceImpl implements AdminsService {

    @Autowired
    private AdminsMapper adminsMapper;

    @Autowired
    private AdminMapper adminMapper;


    @Override
    public ResultVo login(String username, String password) {
        Admins a = new Admins();
        a.setAUsername(username);
        Admins admins = adminsMapper.selectOne(a);
//        Admins admins = adminsMapper.login(username);
        if (admins == null) {
            return new ResultVo(1000, "用户名不存在", null);
        } else {
            if (admins.getAPassword().equals(password)) {
                return new ResultVo(1002, "登录成功", admins);
            } else {
                return new ResultVo(1001, "密码错误", null);
            }
        }
    }

    // 管理员的增删查改
    @Override
    public ResultVo show_admins(String username, int pageNum) {

        // 设置分页    当前页   页面容量(一页有三条记录)
        PageHelper.startPage(pageNum, 3);

        // 条件查询 tk包的example
        Example example = new Example(Admins.class);
        Example.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            // 当name不为空时  进行模糊查询
            criteria.andLike("aUsername", '%' + username + '%');
        }
        List<Admins> list = adminsMapper.selectByExample(example);
        PageInfo<Admins> pageInfo = new PageInfo<Admins>(list);
        return new ResultVo(1002, "成功", pageInfo);

    }

    @Override
    public ResultVo add_admins(Admins admins) {
        // 返回影响的行数
        int i = adminsMapper.insert(admins);
        if (i > 0) {
            return new ResultVo(1002, "增加成功", i);
        } else {
            return new ResultVo(1000, "增加失败", null);
        }
    }

    @Override
    public ResultVo delete_admins(int id) {
        // 通过组件删除
        int i = adminsMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            return new ResultVo(1002, "删除成功", i);
        } else {
            return new ResultVo(1000, "删除失败", null);
        }
    }

    @Override
    public ResultVo update_admins(Admins admins) {
        int i = adminsMapper.updateByPrimaryKeySelective(admins);
        if (i > 0) {
            return new ResultVo(1002, "修改成功", i);
        } else {
            return new ResultVo(1000, "修改失败", null);
        }
    }


    // 学院管理的增删查改
    @Override
    public ResultVo show_department(String name, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<AdminDepartment> adminDepartmentList = adminMapper.show_department(name);
        PageInfo page = new PageInfo(adminDepartmentList);
        if (adminDepartmentList.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo add_department(String name) {
        int i = adminMapper.add_department(name);
        if (i > 0) {
            return new ResultVo(1002, "添加成功", i);
        } else {
            return new ResultVo(1000, "添加失败", null);
        }
    }

    @Override
    public ResultVo edit_department(int id, String name) {
        int i = adminMapper.edit_department(id, name);
        if (i > 0) {
            return new ResultVo(1002, "添加成功", i);
        } else {
            return new ResultVo(1000, "添加失败", null);
        }
    }

    @Override
    public ResultVo delete_department(int id) {
        int i = adminMapper.delete_department(id);
        if (i > 0) {
            return new ResultVo(1002, "添加成功", i);
        } else {
            return new ResultVo(1000, "添加失败", null);
        }
    }


    // 专业管理的增删查改
    @Override
    public ResultVo show_processional(String name, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<AdminProcessional> adminDepartmentList = adminMapper.show_processional(name);
        PageInfo page = new PageInfo(adminDepartmentList);
        if (adminDepartmentList.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo add_processional(int id, String name) {
        int i = adminMapper.add_processional(id, name);
        if (i > 0) {
            return new ResultVo(1002, "添加成功", i);
        } else {
            return new ResultVo(1000, "添加失败", null);
        }
    }

    @Override
    public ResultVo search() {
        List<AdminProcessionalDialog> search = adminMapper.search();
        if (search.size() > 0) {
            return new ResultVo(1002, "查找成功", search);
        } else {
            return new ResultVo(1000, "添加失败", null);
        }
    }

    @Override
    public ResultVo edit_processional(int id, String name, int region) {
        int i = adminMapper.edit_processional(id, name, region);
        if (i > 0) {
            return new ResultVo(1002, "添加成功", i);
        } else {
            return new ResultVo(1000, "添加失败", null);
        }
    }

    @Override
    public ResultVo delete_processional(int id) {
        int i = adminMapper.delete_processional(id);
        if (i > 0) {
            return new ResultVo(1002, "删除成功", i);
        } else {
            return new ResultVo(1000, "删除失败", null);
        }
    }

    // 班级管理的  增删查改

    @Override
    public ResultVo show_class(String name, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<AdminClass> adminClasses = adminMapper.show_class(name);
        PageInfo page = new PageInfo(adminClasses);
        if (adminClasses.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo search_class() {
        List<AdminClassDialog> search = adminMapper.search_class();
        if (search.size() > 0) {
            return new ResultVo(1002, "查找成功", search);
        } else {
            return new ResultVo(1000, "查找失败", null);
        }
    }

    @Override
    public ResultVo add_class(int id, String name, int grade) {
        int i = adminMapper.add_class(id, name, grade);
        if (i > 0) {
            return new ResultVo(1002, "添加成功", i);
        } else {
            return new ResultVo(1000, "添加失败", null);
        }
    }

    @Override
    public ResultVo edit_class(int id, String name, int grade, int region) {
        Integer i = adminMapper.edit_class(id, name, grade, region);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo delete_class(int id) {
        Integer i = adminMapper.delete_class(id);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }


    // 学生管理的增删改查

    @Override
    public ResultVo show_student(String name, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<AdminStudent> adminStudent = adminMapper.show_student(name);
        PageInfo page = new PageInfo(adminStudent);
        if (adminStudent.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo search_student() {
        List<AdminStudentDialog> search = adminMapper.search_student();
        if (search.size() > 0) {
            return new ResultVo(1002, "查找成功", search);
        } else {
            return new ResultVo(1000, "查找失败", null);
        }
    }

    @Override
    public ResultVo add_student(int id, String name, String num, String sex, String password) {
        int i = adminMapper.add_student(id, name, num, sex, password);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo edit_student(int id, int cid, String name, String num, String sex, String password) {
        Integer i = adminMapper.edit_student(id, cid, name, num, sex, password);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo delete_student(int id) {
        int i = adminMapper.delete_student(id);
        if (i > 0) {
            return new ResultVo(1002, "删除成功", i);
        } else {
            return new ResultVo(1000, "删除失败", null);
        }
    }


    // 教师的增删改查


    @Override
    public ResultVo show_teacher(String name, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<AdminTeacher> adminTeachers = adminMapper.show_teacher(name);
        PageInfo page = new PageInfo(adminTeachers);
        if (adminTeachers.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo search_teacher() {
        List<AdminTeacherDialog> search = adminMapper.search_teacher();
        if (search.size() > 0) {
            return new ResultVo(1002, "查找成功", search);
        } else {
            return new ResultVo(1000, "查找失败", null);
        }
    }

    @Override
    public ResultVo add_teacher(int id, String name, String num, String password) {
        int i = adminMapper.add_teacher(id, name, num, password);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo edit_teacher(int id, String num, String name, String password, int did) {
        int i = adminMapper.edit_teacher(id, num, name, password, did);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo delete_teacher(int id) {
        int i = adminMapper.delete_teacher(id);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }


    // 排课的增删改查

    @Override
    public ResultVo show_course(String name, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<AdminCourse> adminCourses = adminMapper.show_course(name);
        PageInfo page = new PageInfo(adminCourses);
        if (adminCourses.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo search_course() {
        List<AdminCourseDialog> search = adminMapper.search_course();
        if (search.size() > 0) {
            return new ResultVo(1002, "查找成功", search);
        } else {
            return new ResultVo(1000, "查找失败", null);
        }
    }

    @Override
    public ResultVo add_course(int id, String name, int credit, String ttime, int sselect, int max, int grade, String location) {
        int i = adminMapper.add_course(id, name, credit, ttime, sselect, max, grade, location);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo edit_course(int id, String name, int credit, String ttime, int sselect, int max, int grade, String location, int tid) {
        int i = adminMapper.edit_course(id, name, credit, ttime, sselect, max, grade, location, tid);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo delete_course(int id) {
        int i = adminMapper.delete_course(id);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo show_selectresult(String cname, String sname, int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<AdminSelectResult> adminSelectResults = adminMapper.show_selectresult(cname, sname);
        PageInfo page = new PageInfo(adminSelectResults);
        if (adminSelectResults.size() > 0) {
            return new ResultVo(1002, "成功", page);
        }
        return new ResultVo(1000, "失败", null);
    }

    @Override
    public ResultVo update_studentscore(int id, int score) {
        int i = adminMapper.update_studentscore(id, score);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }

    @Override
    public ResultVo delete_selectresult(int id) {
        int i = adminMapper.delete_selectresult(id);
        if (i > 0) {
            return new ResultVo(1002, "成功", i);
        } else {
            return new ResultVo(1000, "失败", null);
        }
    }


}
